package com.ycwl.library;

import android.util.Log;

import com.ycwl.freetv.utils.FileUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Utils {
    public static String TAG = "Utils";

    public static String checkSourceFile(String zipFileName, String regex) {
        ZipFile zfile = null;
        String resStr = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            zfile = new ZipFile(zipFileName);
            Enumeration<? extends ZipEntry> zList = zfile.entries();
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            while (zList.hasMoreElements()) {
                ze = (ZipEntry) zList.nextElement();
                if (ze.isDirectory()) {
                    continue;
                }
                String fileName = ze.getName();
                if (!fileName.matches(regex)) {
                    continue; // 如果当前条目不是目标文件则跳过
                }

                bos = new ByteArrayOutputStream();
                is = new BufferedInputStream(zfile.getInputStream(ze));
                int readLen = 0;
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    bos.write(buf, 0, readLen);
                }
                String sha1 = FileUtils.getSha1(bos.toString());
                is.close();
                bos.close();
                if (sha1 == null) {
                    continue;
                }
                if (fileName.matches(".*playlist_"+sha1+".json$")) {
                    resStr = fileName;
                }
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            if (zfile != null) {
                try {
                    zfile.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

        }
        return resStr;
    }
    public static String ReadFileFromCompressed(String zipFileName, String filename)  {
        ZipFile zfile = null;
        String res = null;
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            zfile = new ZipFile(zipFileName);
            ZipEntry zipEntry = zfile.getEntry(filename);
            if (zipEntry == null) {
                throw new FileNotFoundException(filename+ " Cannot find zip file");
            }
            byte[] buf = new byte[1024];
            if (zipEntry.isDirectory()) {
                return null;
            }

            bos = new ByteArrayOutputStream();
            is = new BufferedInputStream(zfile.getInputStream(zipEntry));
            int readLen = 0;
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                bos.write(buf, 0, readLen);
            }
            res = bos.toString();
            is.close();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            if (zfile != null) {
                try {
                    zfile.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return res;
    }

    public static void copyFromZip(String zipFile, String fileName, String destFileName ) {
        ZipFile zfile = null;
        OutputStream os = null;
        InputStream is = null;
        try {
            zfile = new ZipFile(zipFile);
            ZipEntry zipEntry = zfile.getEntry(fileName);
            byte[] buf = new byte[1024*8];
                if (zipEntry.isDirectory()) {
                    zfile.close();
                    return;
                }
                File destFile =  new File(destFileName);
                os = new BufferedOutputStream(new FileOutputStream(destFile));
                is = new BufferedInputStream(zfile.getInputStream(zipEntry));
                int readLen = 0;
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    os.write(buf, 0, readLen);
                }
                is.close();
                os.close();
        } catch (IOException ignored) {

        } finally {
            if (zfile != null) {
                try {
                    zfile.close();
                } catch (IOException ignored) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignored) {

                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ignored) {

                }
            }
        }
    }

    public static void Unzip(String zipFile, String folderPath) throws IOException {
        ZipFile zfile = null;
        try {
            zfile = new ZipFile(zipFile);

            Enumeration<? extends ZipEntry> zList = zfile.entries();
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            while (zList.hasMoreElements()) {
                ze = (ZipEntry) zList.nextElement();
                if (ze.isDirectory()) {
                    Log.d(TAG, "ze.getName() = " + ze.getName());
                    String dirstr = ze.getName();
                    dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                    Log.d(TAG, "str = " + dirstr);
                    File f = new File(folderPath,dirstr);
                    f.mkdir();
                    continue;
                }
                Log.d(TAG, "ze.getName() = " + ze.getName());
                File destFile = getRealFileName(folderPath, ze.getName());
                OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
                InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
                int readLen = 0;
                while ((readLen = is.read(buf, 0, 1024)) != -1) {
                    os.write(buf, 0, readLen);
                }
                is.close();
                os.close();
            }
            zfile.close();
        } catch (IOException e) {
            if (zfile != null) {
                zfile.close();
            }
            throw new RuntimeException(e);
        }
    }

    public static File getRealFileName(String baseDir, String absFileName) {
        String[] dirs = absFileName.split("/");
        File ret = new File(baseDir);
        String substr = null;
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                substr = dirs[i];
                try {
                    substr = new String(substr.getBytes("8859_1"), "GB2312");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                ret = new File(ret, substr);

            }
            Log.d(TAG, "1ret = " + ret);
            if (!ret.exists())
                ret.mkdirs();
            substr = dirs[dirs.length - 1];
            try {
                substr = new String(substr.getBytes("8859_1"), "GB2312");
                Log.d(TAG, "substr = " + substr);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            ret = new File(ret, substr);
            Log.d(TAG, "2ret = " + ret);
            return ret;
        }
        return ret;
    }

    public static void JsonParse() {

    }

}
